package com.kingdee.eas.base.license.file;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class Util {
	private String section = "kingdee eas";
	private String password = "PELDJ3MSE9WNT6KEDQOMGHG7LDJHVFZX";
	private IniFile inifile;

	public String getStr(String str) {
		byte[] bytes = null;
		try {
			bytes = (str + password).getBytes("ISO8859-1");
			MessageDigest md = MessageDigest.getInstance("MD5");
			bytes = md.digest(bytes);
		} catch (NoSuchAlgorithmException e) {
			throw new Error("Cann't find MD5 Alogorithm to hash the value!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return Utils.encode(bytes);
	}

	public void readLicense(String path) {
		try {
			inifile = new IniFile(path);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}
	}

	public IniFile createIniFile(String path) {
		IniFile rs = null;
		try {
			return new IniFile(path);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public void modifyLicenseType(String LicenseType) {
		int len = LicenseType.length();
		String enryptedPart = LicenseType.substring(0, len - 16);
		String hashValue = getStr(enryptedPart);
		setString("LicenseType", enryptedPart + hashValue.substring(0, 16));

		String ax = enryptedPart + hashValue.substring(0, 16)
				+ getString("UnPackageType") + getString("PackageModuleType")
				+ getString("LicenseID") + getString("Trial") // +sdf.format(data.get(i).getExpireDate());sdf.format(data.get(i).getExpireDate());sdf.format(data.get(i).getExpireDate());																// getString("OldFormat")
				+ getString("SubSystemType") + getString("ProductVersion");
		String ChecksumPack = getStr(ax);
		setString("ChecksumPack", ChecksumPack.substring(0, 16));
	}
	
	public void modifyExpireDate(String ExpireDate) {
		String hashValue = getStr(ExpireDate);
		setString("ExpireDate", ExpireDate + hashValue.substring(0, 16));
		
		String ax=getString("LiberationKey")+getString("Cells")+
				getString("LastRunDate")+getString("InitialDate")+
				ExpireDate + hashValue.substring(0, 16)+getString("Counter");
		String Checksum = getStr(ax);
		setString("Checksum", Checksum.substring(0, 16));
	}
	
	public void modifyInitialDate(String InitialDate){
		String hashValue = getStr(InitialDate);
		setString("InitialDate", InitialDate + hashValue.substring(0, 16));
	}
	
	public void modifyValidityExpireDate(String ValidityExpireDate){
		String hashValue = getStr(ValidityExpireDate);
		setString("ValidityExpireDate", ValidityExpireDate + hashValue.substring(0, 16));
	}
	
	public void backup(){
		try {
			inifile.backup();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getString(String property) {
		return inifile.getProperty(section, property);
	}

	public IniFile getInifile() {
		return inifile;
	}

	public void setString(String property, String value) {
		try {
			inifile.setProperty(section, property, value);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Map<String,String> getNumberMapping(){
		Map<String,String> mappingList=new HashMap<String,String>();
		mappingList.put("0","15BA");mappingList.put("1","0DF4");mappingList.put("2","032F");
		mappingList.put("3","04EB");mappingList.put("4","0E5B");mappingList.put("5","0B65");
		mappingList.put("6","06F6");mappingList.put("7","0C8E");mappingList.put("8","0198");
		mappingList.put("9","1420");mappingList.put(":","06C6");mappingList.put("-","11FE");
		mappingList.put(" ","05F0");
		return mappingList;
	}
	
	public static String toCode(String number){
		String code="";
		for(int i=0;i<number.length();i++){
			String si=String.valueOf(number.charAt(i));
			code+=getNumberMapping().get(si);
		}
		return code;
	}
	
	public static void main(String[] args) {
		String a = "abcdefghiWD-WCAM9377071205.01C05";
		String b = "009D10CF12FE0ED004AA0C9500E710A3131905590E8311FE05590A260E74114D142004EB0C8E0C8E15BA0C8E0DF4032F15BA0B65079D15BA0DF40A2615BA0B65C368758E47531BA7";
		System.out.println(a.length() +"   "+(b.length()-16));
	}
}
